home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3059 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Split Filename
  5. Date: 25 Jan 1996 19:54:12 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Distribution: world
  8. Message-ID: <4e8n54$h2s@news.iag.net>
  9. References: <4e6lsr$8ar@news1.radix.net>
  10. NNTP-Posting-Host: pm3-orl17.iag.net
  11. X-Newsreader: WinVN 0.99.7
  12.  
  13. In article <4e6lsr$8ar@news1.radix.net>, jfw@radix.net says...
  14. >
  15. >Is there a C function that will split
  16. >/home/file.c into /home and file.c ?
  17. >
  18. >Thanks,
  19. >
  20. >Jim Ward
  21.  
  22. No ansi c function, but you may find strrchr useful for this.   The 
  23. following code makes a lot of assumptions (ie, it only works safely, because
  24. I know the contents of buf ahead of time and they can't change).  To be 
  25. useful you will need to incorporate a number of safety checks in your code
  26. to protect your array bounds, check for invalid entries, etc.
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. int main()
  32.    {
  33.    char *ptr, file[13], path[50], buf[]= "/root/sub/file.nam";
  34.  
  35.    ptr= strrchr( buf, '/');
  36.    *ptr = '\0';
  37.    ptr++;
  38.  
  39.    strcpy( path, buf);
  40.    strcpy( file, ptr);
  41.  
  42.    printf( "The path is:      %s\n", path);
  43.    printf( "The file name is: %s\n", file);
  44.  
  45.    return 0;
  46.    }
  47.  
  48. -- 
  49. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  50. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  51.  
  52.